package com.example.rdiobuttonmenu;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.rdiobuttonmenu.fragment.BetterFragment;
import com.example.rdiobuttonmenu.fragment.ChannelFragment;
import com.example.rdiobuttonmenu.fragment.MessageFragment;
import com.example.rdiobuttonmenu.fragment.MyFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    float start, end;
    int index;
    RadioButton[] rb = new RadioButton[4];
    FragmentManager fragmentManager;
    Fragment fragmentNow;
    TextView tv_title;
    RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init_view();
        addFragment();
    }

    //初始化组件
    void init_view() {
        rb[0] = findViewById(R.id.rb_channel);
        rb[1] = findViewById(R.id.rb_message);
        rb[2] = findViewById(R.id.rb_better);
        rb[3] = findViewById(R.id.rb_my);
        rb[0].setOnClickListener(this);
        rb[1].setOnClickListener(this);
        rb[2].setOnClickListener(this);
        rb[3].setOnClickListener(this);
        tv_title = findViewById(R.id.tv_title);
        radioGroup = findViewById(R.id.rg);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rb_channel:
                addRadioButton(0);
                index = 0;
                break;
            case R.id.rb_message:
                addRadioButton(1);
                index = 1;
                break;
            case R.id.rb_better:
                addRadioButton(2);
                index = 2;
                break;
            case R.id.rb_my:
                addRadioButton(3);
                index = 3;
                break;
        }
    }

    //添加fragment
    void addFragment() {
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentNow = ChannelFragment.getInstance(rb[0].getText().toString());
        fragmentTransaction.add(R.id.rl, fragmentNow);
        fragmentTransaction.commit();
    }

    //替换fragment
    void upFragment(Fragment fragment) {
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (fragment.isAdded()) {
            fragmentTransaction.hide(fragmentNow).show(fragment);
        } else {
            fragmentTransaction.hide(fragmentNow).add(R.id.rl, fragment);
        }
        fragmentNow = fragment;
        fragmentTransaction.commit();
    }

    //滑动
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            start = event.getX();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            end = event.getX();
            if (start - end > 40) {
                if (index < 3) {
                    index++;
                } else {
                    index = 3;
                }
                rb[index].setChecked(true);
                addRadioButton(index);
            }
            if (end - start > 40) {
                if (index <= 3 && index > 0) {
                    index--;
                } else {
                    index = 0;
                }
                rb[index].setChecked(true);
                addRadioButton(index);
            }
        }
        return super.onTouchEvent(event);
    }

    void addRadioButton(int i) {
        switch (i) {
            case 0:
                upFragment(ChannelFragment.getInstance(rb[0].getText().toString()));
                tv_title.setText(rb[0].getText().toString());
                break;
            case 1:
                upFragment(MessageFragment.getInstance(rb[1].getText().toString()));
                tv_title.setText(rb[1].getText().toString());
                break;
            case 2:
                upFragment(BetterFragment.getInstance(rb[2].getText().toString()));
                tv_title.setText(rb[2].getText().toString());
                break;
            case 3:
                upFragment(MyFragment.getInstance(rb[3].getText().toString()));
                tv_title.setText(rb[3].getText().toString());
                break;
        }
    }
}
